import * as zrender from "zrender";
import { barHeight, barMargin, halfUnitWidth, timeScaleHeight, unitWidth } from "../../config";
import { isHoliday } from "../../utils/holiday";
import { hachureLines } from "./hachure";
import { createFlagGroup } from "./flag";

/**
 * 绘制垂直网格线
 * @param {Object} zr ZRender实例
 * @param {Object} chartState 图表状态对象
 * @param {Array} tasks 任务数组
 * @param {Function} redrawChart 重绘图表函数
 */
export function drawVerticalGridLines(
  zr: any,
  chartState: ChartState,
  tasks: Task[],
  redrawChart: RedrawChartFunction
) {
  // 网格线起始X
  const gridStartX = chartState.chartStartX;
  // 网格线结束X
  // const gridEndX = chartState.timeScaleWidth * unitWidth;
  // 网格线数量
  const gridLineCount = chartState.timeScaleWidth + 1;
  // 滚动X偏移量
  const deltaScrollX = Math.floor(chartState.lastScrollX / unitWidth);
  // 视口任务长度
  const viewPortTaskLength = Math.min(tasks.length, (chartState.canvasHeight - chartState.chartStartY) / (barHeight + barMargin));

  // 绘制网格线
  for (let i = 0 + deltaScrollX, count = 0; count < gridLineCount; i++, count++) {
    const gridX = gridStartX + i * unitWidth;

    // 绘制网格线
    const gridLine = new zrender.Line({
      shape: {
        x1: gridX,
        y1: chartState.chartStartY - timeScaleHeight,
        x2: gridX,
        y2: chartState.chartStartY + (barHeight + barMargin) * viewPortTaskLength
      },
      style: {
        stroke: "lightgray"
      }
    });

    // 绘制日期文字和休息日斜线
    if (count < gridLineCount - 1) {
      drawDateText(zr, i, gridX, chartState, redrawChart);
      drawHolidayHachure(zr, i, chartState, viewPortTaskLength);
    }

    zr.add(gridLine);
  }
}


/**
 * 绘制日期文字
 * @param {Object} zr ZRender实例
 * @param {number} i 日期索引
 * @param {number} gridX 网格X坐标
 * @param {Object} chartState 图表状态对象
 * @param {Function} redrawChart 重绘图表函数
 */
function drawDateText(zr: any, i: number, gridX: number, chartState: ChartState, redrawChart: RedrawChartFunction) {
  const now = +new Date('2025-07-01');
  const currentDate = now + i * 60 * 1000 * 60 * 24;
  const dateInfo = isHoliday(new Date(currentDate));

  const dateText = new zrender.Text({
    style: {
      text: dateInfo.dateString,
      x: gridX,
      y: chartState.chartStartY - timeScaleHeight,
    },
    z: 1
  });


  // 点击事件处理 - 移除里程碑相关功能
  dateText.on('click', function () {
    // 里程碑功能已移除
  });

  // 创建标志组
  const [flagGroup] = createFlagGroup(zr, gridX, halfUnitWidth, chartState.chartStartY, timeScaleHeight);

  // 鼠标悬停事件
  dateText.on('mouseover', function () {
    this.attr({
      style: {
        opacity: 0
      }
    });
    (flagGroup as zrender.Group).show();
  });
  dateText.on('mouseout', function () {
    this.attr({
      style: {
        opacity: 1
      }
    });
    (flagGroup as zrender.Group).hide();
  });

  // 调整文字位置
  const { width, height } = dateText.getBoundingRect();
  dateText.attr({
    style: {
      x: gridX - width / 2 + halfUnitWidth,
      y: chartState.chartStartY - timeScaleHeight - height / 2 + timeScaleHeight / 2,
    }
  });

  zr.add(dateText);
}


/**
 * 绘制休息日斜线
 * @param {Object} zr ZRender实例
 * @param {number} i 日期索引
 * @param {Object} chartState 图表状态对象
 * @param {number} viewPortTaskLength 视口任务长度
 */
function drawHolidayHachure(zr: any, i: number, chartState: ChartState, viewPortTaskLength: number) {
  const now = +new Date('2025-07-01');
  const currentDate = now + i * 60 * 1000 * 60 * 24;
  const dateInfo = isHoliday(new Date(currentDate));

  if (dateInfo.isHoliday) {
    try {
      const lines = hachureLines([
        [chartState.chartStartX + i * unitWidth, chartState.chartStartY],
        [chartState.chartStartX + i * unitWidth + unitWidth, chartState.chartStartY],
        [chartState.chartStartX + i * unitWidth + unitWidth, chartState.chartStartY + (barHeight + barMargin) * viewPortTaskLength],
        [chartState.chartStartX + i * unitWidth, chartState.chartStartY + (barHeight + barMargin) * viewPortTaskLength]
      ], 10, 45);

      lines.forEach(line => {
        const [x1, y1] = line[0];
        const [x2, y2] = line[1];
        const l = new zrender.Line({
          shape: {
            x1, y1, x2, y2
          },
          style: {
            stroke: 'rgba(221, 221, 221, 0.7)'
          }
        });
        zr.add(l);
      });
    } catch (error) {
      // 静默处理错误
    }
  }
}